//
// Ishtar C++ Utility Library
//
// Task Facility - Thread Group Header
//
// NOTE: ThreadGroup generate new thread for each submitted work.
//       They may be aborted at the same time, which needs the work designer
//       to place "abort check point".
//

#ifndef __ISHTAR_TASK_THREAD_GROUP_H
#define __ISHTAR_TASK_THREAD_GROUP_H

#include <Ishtar/Ishtar.h>

#if defined( ISHTAR_COMPILER_HAS_PRAGMA_ONCE )
#pragma once
#endif

#include <boost/noncopyable.hpp>
#include <Ishtar/Task/TaskFwd.h>
#include <Ishtar/Task/WorkExecutor.h>


namespace Ishtar
{

namespace Task
{

///////////////////////////////////////////////////////////////////////////////
//
// Thread Group
//

class ThreadGroupImpl;

class ThreadGroup : public WorkExecutor
{
public:
    ThreadGroup();

    void Shutdown();

    //
    // Generate New Thread
    //

    // add a task work, which is executed by a new thread.
    void SubmitWork( const Work& w );

    // add a new routine thread.
    void AddThread(
        const std::string& name,
        const ExecuteFunction& execFunc,
        Uint interval = DEFAULT_INTERVAL
    );
    
    void AddLoopThread(
        const std::string& name,
        const WorkFunction& workFunc,
        Uint interval = DEFAULT_INTERVAL
    );
    
    void AddOnceThread(
        const std::string& name,
        const WorkFunction& workFunc
    );


    //
    // Accessors
    //

    Uint GetNumAliveThreads() const;
    
    Uint GetNumWaitingWorks() const { return 0; }
      // NOTE: ThreadGroup creates a new thread for each work,
      //       therefore no work would be waiting.
    
    
private:

    WorkExecutorImpl* GetImpl() const;

    boost::scoped_ptr< ThreadGroupImpl > m_impl;

};


///////////////////////////////////////////////////////////////////////////////

} // namespace Task

} // namespace Ishtar

#endif // __ISHTAR_TASK_THREAD_GROUP_H

